package cn.dengta.webapp.im.dto;

import java.util.Map;

import cn.dengta.context.model.WesKit;
import cn.dengta.context.rpc.CachedConfig;
import cn.dengta.context.rpc.CachedConfigManager;
import lombok.Getter;
import lombok.Setter;
import me.codeplayer.util.X;

/**
 * 一些公共配置可以放这里
 */
@Getter
@Setter
public class ImCommonConfig {

	/** 电视墙配置 */
	@Setter
	PushConfig pushConfig;
	/** 速配配置 */
	SpeedDatingConfig speedDatingConfig;
	/** Ai配置 */
	AiConfigDTO aiConfig;
	/** 聊天相关配置 */
	MsgConfigDTO msgConfig;

	public static ImCommonConfig getDefault() {
		final ImCommonConfig config = new ImCommonConfig();
		config.setPushConfig(new PushConfig());
		config.setSpeedDatingConfig(new SpeedDatingConfig());
		config.setAiConfig(AiConfigDTO.findDefault());
		config.setMsgConfig(MsgConfigDTO.findDef());
		return config;
	}

	public AiConfigDTO getAiConfig() {
		if (aiConfig == null) {
			return AiConfigDTO.findDefault();
		}
		return aiConfig;
	}

	public MsgConfigDTO getMsgConfig() {
		if (msgConfig == null) {
			msgConfig = MsgConfigDTO.findDef();
		}
		return msgConfig;
	}

	public static CachedConfig<ImCommonConfig> cache() {
		return ImCommonConfig.InstanceHolder.instance;
	}

	public static SpeedDatingConfig buildSpeedDatingConfig() {
		return cache().get().getSpeedDatingConfig();
	}

	public static AiConfigDTO buildAiConfig() {
		return cache().get().getAiConfig();
	}

	public static MsgConfigDTO buildMsgConfig() {
		return cache().get().getMsgConfig() == null ? MsgConfigDTO.findDef() : cache().get().getMsgConfig();
	}

	private static final class InstanceHolder {

		static final CachedConfig<ImCommonConfig> instance = CachedConfigManager.create(ImCommonConfig.class, ImCommonConfig::getDefault);

	}

	@Getter
	@Setter
	public static class PushConfig {

		private Map<WesKit, WsConfig> wsConfig;

		public WsConfig findWsConfig(WesKit wesKit) {
			final WsConfig con = X.isValid(wsConfig) ? wsConfig.get(wesKit) : new WsConfig();
			return con == null ? new WsConfig() : con;
		}

	}

	@Getter
	@Setter
	public static class WsConfig {

		/** 单次赠送礼物满足上电视条件的钻石数量 **/
		int tvWallGiftCoin = 50;
		/** 单次赠送礼物满足飘屏条件的钻石数量 **/
		int floatScreenGiftCoin = 1000;

	}

}
